require('../css/mine-edit.less')
require('../css/common.css')

// 入口函数
document.ready(function () {
  //调用登录拦截
  window.$utils.checkLogin()
  //用户id
  let user=JSON.parse(localStorage.getItem("user")).userId
  // 1.0 点击返回
  const backBox = document.querySelector('#back')
  backBox.addEventListener('click', function () {
    history.back()
  })
  //页面加载获取用户信息
window.$http.get("/users/accountinfo?userId="+user,function(res){
  console.log(res);
  showCity.textContent=res.data.address.replace(",","")
  js_input.value=res.data.nickname
  weui_textarea.value=res.data.sign
  // console.log(res.data.birthday.indexOf("T"))
  let str=res.data.birthday.slice(0,10)
  showTime.textContent=str
  showPicker.textContent=res.data.gender
})
  
  let js_input=document.querySelector("#js_input")

  let weui_textarea=document.querySelector(".weui-textarea")

  // 2.0 选择性别
  const showPicker = document.querySelector('#showPicker')
  showPicker.addEventListener('click', function () {
    weui.picker([{
      label: '男',
      value: 0
    }, {
      label: '女',
      value: 1
    }], {
      onConfirm: function (result) {
        console.log(result);
        showPicker.textContent=result[0].label
        person.gender=result[0].label
      }
    })
  })

  // 3.0 生日选择
  const showDatePicker = document.querySelector('#showDatePicker')
  const showTime = document.querySelector('#showTime')
  showDatePicker.addEventListener('click', function () {
    weui.datePicker({
      start: 1990,
      end: new Date().getFullYear(),
      onConfirm: function (result) {
        console.log(result);
        let str=""
        let arr=[]
        result.forEach(function(i){
        arr.push(window.$utils.patchzero(i.value+""))
        str=arr.join("-")
         
        })
        // console.log(str);
        showTime.textContent=str
        person.birthday =showTime.textContent
      }
    })
  })
//4.0选择省份
const showCityPicker=document.querySelector("#showCityPicker")
const showCity=document.querySelector("#showCity")
showCityPicker.addEventListener("click",function(){
  const arr1=[]
  window.$http.get("/address/province",function(res){
    console.log(res);
    if(res.status===0){
      res.data.forEach(function(item){
        arr1.push({
          label:item.name,
          value:item.addressId
        })
      })
       // 遍历整理好了数据之后再渲染列表选择
      weui.picker(arr1, {
        // 确认之后会触发这个事件
        onConfirm: function (data1) {
          console.log(data1);
          // 存储省份的值
            // console.log('省份是--->', data1);
            // 在这里还得需要再根据pid,也就是当前这个数组对象中的value值再次请求
          window.$http.get("/address/city/"+data1[0].value,function(res){
            console.log(res);
            const arr2=[]
            if(res.status===0){
              res.data.forEach(function(item){
                arr2.push({
                  label:item.name,
                  value:item.addressId
                })
              })
              console.log(arr2);
              // 再初始化 weui.picker
              setTimeout(function(){
                weui.picker(arr2,{
                  onConfirm: function (data2) {
                    console.log(data2);
                    showCity.textContent = data1[0].label + data2[0].label
                    person.address  = [data1[0].label,data2[0].label]
                  }
                }) 
              },500)
              
            }
          })
        }
      })
    }
  })
})

//点击确定
let showTooltips=document.querySelector("#showTooltips")
showTooltips.addEventListener("click",function(){
  person.nickname=js_input.value
  person.sign=weui_textarea.value
  window.$http.post("/users/userEdit",person,function(res){
      console.log(res);
    if(res.status===0){
      window.$utils.toast(res.status,res.msg,"个人资料上传",function(){
        window.localStorage.setItem("person",JSON.stringify(person))
        location.href="../mine.html"
      })
      
    }else{
      window.$utils.toast(res.status,res.msg)
    }
  })
})
// 声明一个存储个人信息的对象
let personData=JSON.parse(localStorage.getItem("person"))
let person = {
  nickname: personData.nickname,
  gender:personData.gender,
  birthday:personData.birthday,
  address :personData.address,
  sign:personData.sign,
  userId:user
}
//实时更新输入内容长度
let texBox=document.querySelector("#txt-box")
let newBox=document.querySelector("#new-box")
texBox.addEventListener("input",function(){
  newBox.textContent=this.value.length
})


})

